package user

import (
	"cftgo/cftf/db"
	"cftgo/cftf/ginhelper"
	"login/internal/common/constant"
	"login/model"
	"login/service/jwt"
	"strings"

	"github.com/gin-gonic/gin"
)

type User struct {
}

func (this *User) Info(c *gin.Context) {
	authHeader := c.Request.Header.Get("Authorization")
	tokenString := strings.TrimPrefix(authHeader, "Bearer ")
	if claims, err := jwt.ValidateToken(tokenString); err != nil {
		ginhelper.RspErr(c, 1, "params format error")
		return
	} else {
		rspObj := &struct {
			Aid          string  `json:"aid"`
			AppId        int32   `json:"app_id"`
			PlatId       int32   `json:"plat_id"`
			PlatOpenId   string  `json:"plat_openid"`
			PlatUnionId  *string `json:"plat_unionid"`
			RegisterTime int64   `json:"register_time"`
		}{}
		accModel := new(model.Account)
		result := db.GetOrmDb(constant.ACCOUNT_DB).First(&accModel, map[string]interface{}{
			"aid": claims.Subject,
		})
		if result.Error != nil {
			ginhelper.RspErr(c, 500, "server internal error")
			return
		}
		rspObj.Aid = accModel.Aid.String()
		rspObj.AppId = accModel.AppId
		rspObj.PlatId = accModel.PlatId
		rspObj.PlatOpenId = accModel.PlatOpenId
		rspObj.PlatUnionId = accModel.PlatUnionId
		rspObj.RegisterTime = accModel.CreatedAt
		ginhelper.RspData(c, rspObj)
	}
}
